package com.linewow.xhyy.basiclibrary.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linewow.xhyy.basiclibrary.entity.TagInfo;
import com.linewow.xhyy.basiclibrary.util.CommonUtil;

/**
 * Created by LXR on 2016/9/7.
 */
public class ColorfulTag extends RelativeLayout {
    private LayoutInflater layoutInflater;
    private TextView tv;
    private String TAG="ColorfulTag";
    private int marginLeft,marginTop,round,stroke;
    public ColorfulTag(Context context) {
        super(context);
        init(context,null);
    }

    public ColorfulTag(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public ColorfulTag(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        layoutInflater= LayoutInflater.from(context);
        tv=new TextView(context);
        marginLeft= CommonUtil.dp2px(context,6);
        marginTop=CommonUtil.dp2px(context,2);
        round=CommonUtil.dp2px(context,5);
        stroke=CommonUtil.dp2px(context,1);
//        marginLeft=12;
//        marginTop=4;
//        round=30;
//        stroke=2;
        Log.e(TAG,"大小"+CommonUtil.dp2px(context,6));
        tv.setTextSize(15);
        RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setMargins(marginLeft,marginTop,marginLeft,marginTop);
        tv.setLayoutParams(lp);
        addView(tv);
    }

    public void setContent(TagInfo info){
        tv.setText(info.tag);
        if(info.color.startsWith("#")){
            Log.e(TAG,"color"+info.color);
            tv.setTextColor(Color.parseColor("#7d8794"));//30, 30, 30, 30, 30, 30, 30, 30
            RoundRectShape rs = new RoundRectShape(new float[] {round, round, round, round, round, round, round, round}, null, null);
            ShapeDrawable sdOff = new CustomShapeDrawable(rs, Color.WHITE, Color.parseColor(info.color), stroke);
            StateListDrawable stld = new StateListDrawable();
            stld.addState(new int[] { android.R.attr.state_enabled }, sdOff);
            setBackground(stld);
        }
        setTag(info.tag);
    }
    public void setSimpleContent(TagInfo info){
        tv.setText(info.tag);
        tv.setTextColor(Color.parseColor("#7d8794"));//30, 30, 30, 30, 30, 30, 30, 30
        RoundRectShape rs = new RoundRectShape(new float[] {round, round, round, round, round, round, round, round}, null, null);
        ShapeDrawable sdOff = new CustomShapeDrawable(rs, Color.WHITE, Color.parseColor("#e7eaf1"), stroke);
        StateListDrawable stld = new StateListDrawable();
        stld.addState(new int[] { android.R.attr.state_enabled }, sdOff);
        setBackground(stld);
        setTag(info.tag);
    }

    public void setHei(int p){
        Log.e(TAG,"高度"+p);
        tv.setHeight(p);
    }
}
